home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / wb / WBTRIS_1_54.lha / WBTRIS_1.54 / Source / Hiscore.c < prev    next >
C/C++ Source or Header  |  1993-03-27  |  8KB  |  309 lines

  1. #include "WBTRIS.h"
  2.  
  3. struct HiscorePart {
  4.    int  Position;
  5.    char Name[40];
  6.    int  Score;
  7.    int  Rows;
  8.    int  Level;
  9. };
  10.  
  11. struct RastPort       *HiscoreRP = NULL;
  12. struct HiscorePart     Hiscore[10];
  13. struct Window         *HiscoreWindow = NULL;
  14.  
  15. short                  win_width;
  16. short                  win_height;
  17.  
  18. extern BOOL            UseLace;
  19. extern struct Screen  *myscreen;
  20. extern struct TextAttr helvetica13;
  21. extern struct TextAttr topaz8;
  22.  
  23. void HiscoreList(char *Name, int Level, int Score, int Rows, int XOffset, int YOffset, BOOL ShowHiscore)
  24. {
  25.    struct IntuiMessage *imsg = NULL;
  26.    BOOL                 terminated = FALSE;
  27.  
  28.    if (UseLace) {
  29.       win_width = MY_WIN_WIDTH;
  30.       win_height = MY_WIN_HEIGHT + 7;
  31.    } else {
  32.       win_width = MY_WIN_WIDTH+32;
  33.       win_height = MY_WIN_HEIGHT - 10;
  34.    }
  35.  
  36.    if (HiscoreWindow = OpenWindowTags(NULL,
  37.                                 WA_Left,       XOffset,
  38.                                 WA_Top,        YOffset+(myscreen->Font->ta_YSize)+3,
  39.                                 WA_Width,      win_width,
  40.                                 WA_Height,     win_height+(myscreen->Font->ta_YSize),
  41.                                 WA_Title,      "<-- Click to close!",
  42.                                 WA_Flags,      WFLG_CLOSEGADGET | WFLG_ACTIVATE | WFLG_DRAGBAR | WFLG_DEPTHGADGET,
  43.                                 WA_IDCMP,      IDCMP_CLOSEWINDOW | IDCMP_RAWKEY,
  44.                                 TAG_END))
  45.       {
  46.       HiscoreRP = HiscoreWindow->RPort;
  47.  
  48.       LoadFile();
  49.       UpdateHiscore(Name, Score, Rows, Level);
  50.       OutHiscoreList();
  51.       if (!ShowHiscore) {
  52.          if (SaveFile() == FALSE)
  53.             CloseWindow(HiscoreWindow);
  54.       }
  55.  
  56.    while (!terminated) {
  57.       Wait (1 << HiscoreWindow->UserPort->mp_SigBit);
  58.  
  59.       while (imsg = GT_GetIMsg(HiscoreWindow->UserPort)) {
  60.          switch (imsg->Class) {
  61.             case IDCMP_RAWKEY:
  62.                if (imsg->Code == '\x45')
  63.                   terminated = TRUE;
  64.                break;
  65.  
  66.             case IDCMP_CLOSEWINDOW:
  67.                terminated = TRUE;
  68.                break;
  69.          }
  70.          GT_ReplyIMsg(imsg);
  71.       }
  72.    }
  73.    CloseWindow(HiscoreWindow);
  74.    }
  75. }
  76.  
  77.  
  78.  
  79. /*
  80. ** Ausgabe der Liste ins Fenster
  81. */
  82. void OutHiscoreList(void)
  83. {
  84.    int              i;
  85.    char             s[80];
  86.    struct IntuiText Zeile;
  87.    short            Step = 0;
  88.  
  89.    if (UseLace)
  90.       Step = 2;
  91.    Zeile.FrontPen = 1;
  92.    Zeile.BackPen = 0;
  93.    Zeile.DrawMode = JAM2;
  94.    Zeile.LeftEdge = 0;
  95.    Zeile.TopEdge = 0;
  96.    if (UseLace)
  97.       Zeile.ITextFont = &helvetica13;
  98.    else
  99.       Zeile.ITextFont = &topaz8;
  100.    Zeile.NextText = NULL;
  101.  
  102.    strcpy(s, "Pos.");
  103.    Zeile.IText = s;
  104.    PrintIText(HiscoreRP, &Zeile, 12, 8+(myscreen->Font->ta_YSize));
  105.  
  106.    strcpy(s, "Name");
  107.    Zeile.IText = s;
  108.    if (UseLace)
  109.       PrintIText(HiscoreRP, &Zeile, 40, 8+(myscreen->Font->ta_YSize));
  110.    else
  111.       PrintIText(HiscoreRP, &Zeile, 50, 8+(myscreen->Font->ta_YSize));
  112.  
  113.    strcpy(s, "Score");
  114.    Zeile.IText = s;
  115.    PrintIText(HiscoreRP, &Zeile, 210, 8+(myscreen->Font->ta_YSize));
  116.  
  117.    strcpy(s, "Rows");
  118.    Zeile.IText = s;
  119.    if (UseLace)
  120.       PrintIText(HiscoreRP, &Zeile, 250, 8+(myscreen->Font->ta_YSize));
  121.    else
  122.       PrintIText(HiscoreRP, &Zeile, 265, 8+(myscreen->Font->ta_YSize));
  123.  
  124.    strcpy(s, "Level");
  125.    Zeile.IText = s;
  126.    if (UseLace)
  127.       PrintIText(HiscoreRP, &Zeile, 290, 8+(myscreen->Font->ta_YSize));
  128.    else
  129.       PrintIText(HiscoreRP, &Zeile, 313, 8+(myscreen->Font->ta_YSize));
  130.  
  131.    SetAPen(HiscoreRP, 1);
  132.    if (UseLace) {
  133.       Move(HiscoreRP, 10, 23+(myscreen->Font->ta_YSize));
  134.       Draw(HiscoreRP, win_width - 10, 23+(myscreen->Font->ta_YSize));
  135.    }
  136.    else {
  137.       Move(HiscoreRP, 10, 20+(myscreen->Font->ta_YSize));
  138.       Draw(HiscoreRP, win_width - 10, 20+(myscreen->Font->ta_YSize));
  139.    }
  140.  
  141.    for (i=0; i<10; i++) {
  142.       sprintf(s, "%2d", i+1);
  143.       Zeile.IText = s;
  144.       PrintIText(HiscoreRP, &Zeile, 12, 26+(11 + Step)*i+(myscreen->Font->ta_YSize));
  145.  
  146.       sprintf(s, "%-23s", Hiscore[i].Name);
  147.       Zeile.IText = s;
  148.       if (UseLace)
  149.          PrintIText(HiscoreRP, &Zeile, 40, 26+(11 + Step)*i+(myscreen->Font->ta_YSize));
  150.       else
  151.          PrintIText(HiscoreRP, &Zeile, 50, 26+(11 + Step)*i+(myscreen->Font->ta_YSize));
  152.  
  153.       sprintf(s, "%6d", Hiscore[i].Score);
  154.       Zeile.IText = s;
  155.       PrintIText(HiscoreRP, &Zeile, 203, 26+(11 + Step)*i+(myscreen->Font->ta_YSize));
  156.  
  157.       sprintf(s, "%5d", Hiscore[i].Rows);
  158.       Zeile.IText = s;
  159.       PrintIText(HiscoreRP, &Zeile, 250, 26+(11 + Step)*i+(myscreen->Font->ta_YSize));
  160.  
  161.       sprintf(s, "%3d", Hiscore[i].Level);
  162.       Zeile.IText = s;
  163.       if (UseLace)
  164.          PrintIText(HiscoreRP, &Zeile, 290, 26+(11 + Step)*i+(myscreen->Font->ta_YSize));
  165.       else
  166.          PrintIText(HiscoreRP, &Zeile, 313, 26+(11 + Step)*i+(myscreen->Font->ta_YSize));
  167.  
  168.    }
  169. }
  170.  
  171.  
  172.  
  173. /*
  174. ** Sortiert Liste neu
  175. */
  176. void UpdateHiscore(char *Name, int Score, int Rows, int Level)
  177. {
  178.    int i = 0;
  179.    int j;
  180.    BOOL equal = FALSE;
  181.  
  182.    while ((equal == FALSE) && (Score <= Hiscore[i].Score) && (i <= 9)) {
  183.       if (Score == Hiscore[i].Score)
  184.          equal = TRUE;
  185.       else
  186.          i++;
  187.    }
  188.  
  189.    if (equal == TRUE) {
  190.       while ((Score == Hiscore[i].Score) && (Rows <= Hiscore[i].Rows) && (i<=9))
  191.          i++;
  192.    }
  193.  
  194.    for (j = 9; j > i; j--) {
  195.       Hiscore[j].Score = Hiscore[j-1].Score;
  196.       strcpy(Hiscore[j].Name, Hiscore[j-1].Name);
  197.       Hiscore[j].Rows  = Hiscore[j-1].Rows;
  198.       Hiscore[j].Level  = Hiscore[j-1].Level;
  199.    }
  200.  
  201.    Hiscore[i].Score = Score;
  202.    strcpy(Hiscore[i].Name, Name);
  203.    Hiscore[i].Rows  = Rows;
  204.    Hiscore[i].Level = Level;
  205. }
  206.  
  207.  
  208.  
  209. /*
  210. ** Speichert das Hiscorefile ab
  211. */
  212. BOOL SaveFile(void)
  213. {
  214.    int   i;
  215.    FILE *fp = NULL;
  216.    char  FName[80];
  217.  
  218.    if (getenv("WBTRIS"))
  219.       strcpy(FName, getenv("WBTRIS"));
  220.    else
  221.       strcpy(FName, FILENAME);
  222.  
  223.    if ((fp = fopen(FName, "w")) == NULL) {
  224.       fprintf(stderr, "Couldn't open file '%s'.\n", FName);
  225.       return(FALSE);
  226.    }
  227.    for (i=0; i<10; i++) {
  228.       fprintf(fp, "%d/%s/%d/%d/%d\n", i+1, Hiscore[i].Name, Hiscore[i].Score, Hiscore[i].Rows, Hiscore[i].Level);
  229.    }
  230.    fclose(fp);
  231.    return(TRUE);
  232. }
  233.  
  234.  
  235.  
  236. /*
  237. ** Laedt das Hiscorefile ein
  238. */
  239. void LoadFile(void)
  240. {
  241.    int i;
  242.    FILE *fp;
  243.    int c;
  244.    char text[30];
  245.    char *ptr;
  246.    char FName[80];
  247.  
  248.    if (getenv("WBTRIS"))
  249.       strcpy(FName, getenv("WBTRIS"));
  250.    else
  251.       strcpy(FName, FILENAME);
  252.  
  253.    if ((fp = fopen(FName, "r")) == NULL) {
  254.       for (i=0; i<10; i++) {
  255.          Hiscore[i].Position = i+1;
  256.          strcpy(Hiscore[i].Name, "...");
  257.          Hiscore[i].Score = 0;
  258.          Hiscore[i].Rows = 0;
  259.          Hiscore[i].Level = 0;
  260.       }
  261.    } else {
  262.       i = 0;
  263.       while ((c = fgetc(fp)) != EOF) {
  264.          while (c != '/') {
  265.             c = fgetc(fp);
  266.          }
  267.          Hiscore[i].Position = i+1;
  268.  
  269.          ptr = &text[0];
  270.          *ptr = '\0';
  271.          while ((c = fgetc(fp)) != '/') {
  272.             *ptr = c;
  273.             ptr++;
  274.          }
  275.          *ptr = '\0';
  276.          strcpy(Hiscore[i].Name, text);
  277.  
  278.          ptr = &text[0];
  279.          *ptr = '\0';
  280.          while ((c = fgetc(fp)) != '/') {
  281.             *ptr = c;
  282.             ptr++;
  283.          }
  284.          *ptr = '\0';
  285.          Hiscore[i].Score = atoi(text);
  286.  
  287.          ptr = &text[0];
  288.          *ptr = '\0';
  289.          while ((c = fgetc(fp)) != '/') {
  290.             *ptr = c;
  291.             ptr++;
  292.          }
  293.          *ptr = '\0';
  294.          Hiscore[i].Rows = atoi(text);
  295.  
  296.          ptr = &text[0];
  297.          *ptr = '\0';
  298.          while ((c = fgetc(fp)) != '\n') {
  299.             *ptr = c;
  300.             ptr++;
  301.          }
  302.          *ptr = '\0';
  303.          Hiscore[i].Level = atoi(text);
  304.          i++;
  305.       }
  306.       fclose(fp);
  307.    }
  308. }
  309.